package com.yc.jpyy.mobile.controller;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yc.jpyy.base.bean.RestResponse;
import com.yc.jpyy.base.controller.BaseController;
import com.yc.jpyy.mobile.dao.BizDao;
import com.yc.jpyy.mobile.service.BizService;

@Controller
@RequestMapping(value = "/question")
public class OperateQuestionController extends BaseController {

	@Autowired
	private BizService bizService;

	@Autowired
	private BizDao bizDao;
	private static Logger logger = LoggerFactory.getLogger(GetClassInformation.class);
	
	
	
	/**
	 *答题记录接口
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addQuestionInfo", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addQuestionInfo(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 用户Id
			String userId = request.getParameter("userId");
			// 试题ID
			String questionId = request.getParameter("questionId");
			//是否答错
			String isError = request.getParameter("isError");
			
			if((userId != null && !userId.isEmpty())
					&& (questionId != null && !questionId.isEmpty())){
				Map<String, Object> hashMap = new HashMap<String, Object>();
				hashMap.put("userId", userId);
				hashMap.put("questionId", questionId);
				hashMap.put("isError", isError);
				//添加答题记录和错题记录
				response = bizService.addQuestionInfo(hashMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("addQuestionInfo:"+ e.toString());
			response.setReturnMsg(1,"添加试题记录异常！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 *试题收藏接口
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addQuestionCollect", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addQuestionCollect(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 用户Id
			String userId = request.getParameter("userId");
			// 试题ID
			String questionId = request.getParameter("questionId");
			//收藏Or取消收藏
			String isCollect = request.getParameter("isCollect");
			
			if((userId != null && !userId.isEmpty())
					&& (questionId != null && !questionId.isEmpty())
					&& (isCollect != null && !isCollect.isEmpty())){
				Map<String, Object> hashMap = new HashMap<String, Object>();
				hashMap.put("userId", userId);
				hashMap.put("questionId", questionId);
				hashMap.put("isCollect", isCollect);
				//收藏试题
				response = bizService.addQuestionCollect(hashMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("addQuestionCollect:"+ e.toString());
			response.setReturnMsg(1,"添加试题收藏记录异常！");
		} finally {
			return response.toJSON();
		}
	}
	
	/**
	 *记录模考接口
	 * 
	 * @param request
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@SuppressWarnings("finally")
	@RequestMapping(value = "/addExamRecord", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
	public @ResponseBody String addExamRecord(HttpServletRequest request)
			throws UnsupportedEncodingException {

		RestResponse response = new RestResponse();
		request.setCharacterEncoding("UTF-8");

		try {
			// 用户Id
			String userId = request.getParameter("userId");
			// 科目
			String subjectId = request.getParameter("subjectId");
			//分数
			String score = request.getParameter("score");
			//学驾类型
			String driverType = request.getParameter("driverType");
			//模考时间
			String examTime =  request.getParameter("examTime");
			
			if((userId != null && !userId.isEmpty())
					&& (subjectId != null && !subjectId.isEmpty())
					&& (score != null && !score.isEmpty())
					&& (driverType != null && !driverType.isEmpty())
					&& (examTime != null && !examTime.isEmpty())){
				Map<String, Object> hashMap = new HashMap<String, Object>();
				hashMap.put("userId", userId);
				hashMap.put("score", score);
				hashMap.put("subjectId", subjectId);
				hashMap.put("driverType", driverType);
				hashMap.put("examTime", examTime);
				//添加模考记录
				response = bizService.addExamRecord(hashMap,response);
			}else{
				response.setErrorcode(1);
				response.setMessage("必填参数为空...");
			}
		} catch (Exception e) {
			logger.info("addExamRecord:"+ e.toString());
			response.setReturnMsg(1,"添加模考记录异常！");
		} finally {
			return response.toJSON();
		}
	}
	
}
